<?php
function log_error($num, $str, $file, $line)
{
    $message = date("Y-m-d H:i:s") . " 文件：{$file},行数：{$line},错误信息：{$str}";
    file_put_contents("../api_resource/logs/" . date("Ymd") . "_api.log", $message . PHP_EOL, FILE_APPEND);
}
set_error_handler("log_error");

include_once '../api_resource/mysql_pdo.php';

include_once '../api_resource/functions.php';


$params = $_POST;
$act = $params['act'] ?? '';
$uid = $params['uid'] ?? 0;
$timestamp = $params['timestamp'] ?? 0;
$sign = $params['sign'] ?? '';
check_base_params($params);

$db = new mysql_pdo();

//获取用户信息
$user = select_table_one($db, 'sys_users', [
    'id' => intval($uid)
]);

if (!$user) {
    error(CzApiEnum::USER_NOT_EXIST, CzApiEnum::USER_NOT_EXIST_CODE);
}


$api_secret = $user['api_secret'];

//检查签名
check_submit_sign($params, $api_secret);

include_once '../api_resource/CzOrder.php';

$CzOrder = new CzOrder($params, $db, $user);
if ($act == 'orderSubmit') {
    $CzOrder->orderSubmit();
}
if ($act == 'queryOrder') {
    $CzOrder->queryOrder();
}
if ($act == 'queryBalance') {
    $CzOrder->queryBalance();
}

if ($act == 'queryProduct') {
    $CzOrder->queryProduct();
}

error('未知请求操作');
